<template>
  <div>
    <el-form ref="form" :model="form" label-width="80px" style="position: absolute;margin-left: 100px;margin-top: 100px;width: 400px;">
      <el-form-item label="药品编号">
        <el-input v-model="form.deptCode"></el-input>
      </el-form-item>
      <el-form-item label="药品名称">
        <el-input v-model="form.deptName"></el-input>
      </el-form-item>
      <el-form-item label="药品类型">
        <el-input v-model="form.deptCategoryName"></el-input>
      </el-form-item>
      <el-form-item label="药品数量">
        <el-input v-model="form.total"></el-input>
      </el-form-item>
      <el-form-item label="是否上架">
        <div>
          <el-radio v-model="form.is_ground" label="1" border>上架</el-radio>
          <el-radio v-model="form.is_ground" label="0" border>不上架</el-radio>
        </div>
      </el-form-item>
      <el-form-item label="药物价格">
        <el-input-number v-model="form.price"></el-input-number>
      </el-form-item>
      <el-form-item label="所属科室">
        <el-select v-model="form.department" multiple placeholder="请选择药品所属科室">
          <el-option v-for="department in all_department" :label=department.name :value=department.id></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="createDepartment">立即创建</el-button>
        <el-button type="danger" plain @click="resetForm('formInline')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import router from "../../router";

export default {
  // inject: ['reload'],
  data() {
    return {
      all_department: [],
      form: {
        deptCode: "",
        deptName: "",
        deptType: "",
        deptCategoryName: "",
        user: {},
        total: "",
        is_ground: "",
        price: "",
        department: "",
      },
    };
  },
  created() {
    // this.$axios.get(this.$settings.BASE_URL + 'user/department/').then(request => {
    this.$axios.get('http://127.0.0.1:8000/api/v1/user/ke_name/').then(request => {
      this.all_department = request.data
      console.log(request.data)
    })
  },
  methods: {
    createDepartment() {
      const list = []
      for (let i = 0; i < this.form.department.length; i++) {
        list.push(this.form.department[i])
      }
      this.user = this.$getSessionStorage("user");
      //axios新建科室请求
      //  # http://127.0.0.1:8000/api/v1/user/yao/
      // this.$axios.post(this.$settings.BASE_URL + "user/medicine/", {
      this.$axios.post('http://127.0.0.1:8000/api/v1/user/yao/', {
        med_number: this.form.deptCode,
        name: this.form.deptName,
        total: this.form.total,
        category: this.form.deptCategoryName,
        is_ground: this.form.is_ground,
        price: this.form.price,
        department: list,
      }).then((Response) => {
        this.$message({
          message: "新建" + this.form.deptName + "药品成功",
          type: 'success',
          duration: 1000
        })
        window.setTimeout(function () {
          router.push({path: '/index/drugAdministration'})
        }, 2000)
      }).catch((error) => {
        console.error(error);
      });
    },
    resetForm(formName) {
      this.reload();
    },
  },
};
</script>